//主程序

#include"util.hpp"
#include"confg.hpp"
#include"data.hpp"
#include"hot.hpp"
#include"service.hpp"
#include<thread>
void FileUtilTest(const std::string &filename){
    /*cloud::FileUtil fu(filename);
    std::cout<<fu.FileSize()<<std::endl;
    std::cout<<fu.LastTime()<<std::endl;
    std::cout<<fu.LastATime()<<std::endl;
    */
    /*cloud::FileUtil fu(filename);
    std::string body;
    fu.GetContent(&body);//读取测试

    cloud::FileUtil ufu("./hello.txt");
    ufu.SetContent(body);
    */
    /*cloud::FileUtil fu(filename);
    fu.Compress(filename + ".lz");
    cloud::FileUtil pfu(filename + ".lz");
    pfu.uncompress("./hello.txt");
    */
    /*cloud::FileUtil fu(filename);
    fu.CreateDirectory();
    std::vector<std::string>arry;
    fu.ScanDirectory(&arry);
    for(auto & a : arry){
        std::cout<<a<<std::endl;
    }
    */

    return ;
}

void JsonUtilTest(){
    const char * name = "小明";
    int age = 18 ;
    float score[]= {77.5 , 88 ,93.6};
    //创建一个Value实例
    Json::Value root;

    //向Value实例中添加数据
    root["姓名"] = name;
    root["年龄"] = age;
    root["成绩"].append(score[0]);
    root["成绩"].append(score[1]);
    root["成绩"].append(score[2]);
    std::string json_str;
    cloud::JsonUtil::Serialize(root , &json_str);
    std::cout<<json_str<<std::endl;

    Json::Value val;
    cloud::JsonUtil::UnSerialize(json_str , &val);
    // 输出反序列化后的数据
    std::cout << "姓名: " << val["姓名"].asCString() << std::endl;
    std::cout << "年龄: " << val["年龄"].asInt() << std::endl;
    std::cout << "成绩: ";
    for (int i = 0; i < val["成绩"].size(); ++i) {
        std::cout << val["成绩"][i].asFloat() << " ";
    }
    std::cout << std::endl;
}

void configtest(){
    cloud::Config *config = cloud::Config::GetInstance();
    std::cout<<config->GetHotTime()<<std::endl;
    std::cout<<config->GetServerPort()<<std::endl;
    std::cout<<config->GetServerIp()<<std::endl;
    std::cout<<config->GetDownloadPrefix()<<std::endl;
    std::cout<<config->GetPackFileSuffix()<<std::endl;
    std::cout<<config->GetPackDir()<<std::endl;
    std::cout<<config->GetBackDir()<<std::endl;
    std::cout<<config->GetBackupFile()<<std::endl;

}

void Datatest(const std::string &filename){
    cloud::DataManager data;
    std::vector<cloud::BackupInfo>arry;
    data.GetAll(&arry);
    for(auto temps: arry){
    std::cout<<temps.pack_flag<<std::endl;
    std::cout<<temps.fsize<<std::endl;
    std::cout<<temps.mtime<<std::endl;
    std::cout<<temps.atime<<std::endl;
    std::cout<<temps.real_path<<std::endl;
    std::cout<<temps.pack_path<<std::endl;
    std::cout<<temps.url<<std::endl;
    }
   /*cloud::BackupInfo info;
    info.NewBackupInfo(filename);
    cloud::DataManager data;
    data.Insert(info);

    std::cout<<"------------url\n";
    cloud::BackupInfo temp;
    data.GetOneByURL("/download/bundle.h", &temp);
    std::cout<<temp.pack_flag<<std::endl;
    std::cout<<temp.fsize<<std::endl;
    std::cout<<temp.mtime<<std::endl;
    std::cout<<temp.atime<<std::endl;
    std::cout<<temp.url<<std::endl;
    std::cout<<temp.real_path<<std::endl;
    std::cout<<temp.pack_path<<std::endl;

    std::cout<<"------------updata and getall";
    info.pack_flag =true;
    data.Update(info);
    std::vector<cloud::BackupInfo>arry;
    data.GetAll(&arry);
    for(auto temps: arry){
        std::cout<<temp.pack_flag<<std::endl;
    std::cout<<temps.fsize<<std::endl;
    std::cout<<temps.mtime<<std::endl;
    std::cout<<temps.atime<<std::endl;
    std::cout<<temps.url<<std::endl;
    std::cout<<temps.real_path<<std::endl;
    std::cout<<temps.pack_path<<std::endl;
    }

    data.GetOneByRealPath(filename , &temp);
    std::cout<<"----------realpath"<<std::endl;
    std::cout<<temp.pack_flag<<std::endl;
    std::cout<<temp.fsize<<std::endl;
    std::cout<<temp.mtime<<std::endl;
    std::cout<<temp.atime<<std::endl;
    std::cout<<temp.url<<std::endl;
    std::cout<<temp.real_path<<std::endl;
    std::cout<<temp.pack_path<<std::endl;

    data.Storage();
    */
}
cloud::DataManager *data;
void Hottest(){
    data = new cloud::DataManager();
    cloud::HotManager hot;
    hot.RunModule();
}

void servertest(){
    cloud::Service srv;
    srv.RunMoudle();
}
int main(int argc , char * argv[]){
    data = new cloud::DataManager();
    //FileUtilTest(argv[1]);
    //JsonUtilTest();
    //configtest();
    //Datatest(argv[1]);
    //Hottest();
    //servertest();
    std::thread thread_hot_manerger(Hottest);
    std::thread thread_service(servertest);
    thread_hot_manerger.join();
    thread_service.join();
    return 0;

}